/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Picker;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.CommonDialog;


import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import org.threeten.bp.LocalDate;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * @since 2021-07-12
 */
public class CustomTileDimensions extends BaseAbility implements Component.ClickedListener, OnDateSelectedListener {
    protected static final int COLOR_TODAY = 0Xff44afcc;
    private List<String> list;
    private CommonDialog widthDialog;
    private CommonDialog heightDialog;
    private Button custom_tile_width_size;
    private Button custom_tile_width_match_parent;
    private Button custom_tile_height_size;
    private Button custom_tile_height_match_parent;
    private Button custom_tile_match_parent;
    private MaterialCalendarView widget;
    private CalendarDay date;

    private int currentTileWidth;
    private int currentTileHeight;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarTitle.setText("Custom tile width/height");
        custom_tile_width_size = (Button) findComponentById(ResourceTable.Id_custom_tile_width_size);
        custom_tile_width_size.setClickedListener(this);
        custom_tile_width_match_parent = (Button) findComponentById(ResourceTable.Id_custom_tile_width_match_parent);
        custom_tile_width_match_parent.setClickedListener(this);
        custom_tile_height_size = (Button) findComponentById(ResourceTable.Id_custom_tile_height_size);
        custom_tile_height_size.setClickedListener(this);
        custom_tile_height_match_parent = (Button) findComponentById(ResourceTable.Id_custom_tile_height_match_parent);
        custom_tile_height_match_parent.setClickedListener(this);
        custom_tile_match_parent = (Button) findComponentById(ResourceTable.Id_custom_tile_match_parent);
        custom_tile_match_parent.setClickedListener(this);
        widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_PRIMARY);
        widget.setOnDateChangedListener(this);
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
                LocalDate mselect = date.getDate();
                LocalDate today = CalendarDay.today().getDate();
                if (mselect.getDayOfYear() == today.getDayOfYear() &&mselect.getDayOfMonth()== today.getDayOfMonth()&&mselect.getMonthValue() == today.getMonthValue()) {
                    widget.setSelectionColor(COLOR_TODAY);
                } else {
                    widget.setSelectionColor(COLOR_ACCENT);
                }
            }
        });

//        date = CalendarDay.today();
        initWidthDialog();
        initHeightDialog();
        widget.addDecorator(new TodayDecorator());
    }


    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_custom_tile;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_custom_tile_width_size:
                widthDialog.show();
                break;
            case ResourceTable.Id_custom_tile_width_match_parent:
                widget.setTileWidth(MATCH_PARENT);
                if (date != null) {
                    widget.setSelectedDate(date);
                } else {
                    date = CalendarDay.today();
                    widget.setSelectedDate(date);
                    widget.clearSelection();
                    date = null;
                }
                break;
            case ResourceTable.Id_custom_tile_height_size:
                heightDialog.show();
                break;
            case ResourceTable.Id_custom_tile_height_match_parent:
                widget.setTileHeight(MATCH_PARENT);
                if (date != null) {
                    widget.setSelectedDate(date);
                } else {
                    date = CalendarDay.today();
                    widget.setSelectedDate(date);
                    widget.clearSelection();
                    date = null;
                }
                break;
            case ResourceTable.Id_custom_tile_match_parent:
                widget.setTileWidth(MATCH_PARENT);
                widget.setTileHeight(MATCH_PARENT);
                if (date != null) {
                    widget.setSelectedDate(date);
                } else {
                    date = CalendarDay.today();
                    widget.setSelectedDate(date);
                    widget.clearSelection();
                    date = null;
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: ");
        }
    }

    private void initWidthDialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_picker, null, false);
        widthDialog = initdialog(mComponent, this);
        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        Picker mPicker = (Picker) mComponent.findComponentById(ResourceTable.Id_picker);
        List<String> list = setData();
        mPicker.setMinValue(0);
        mPicker.setMaxValue(list.size() - 1);
        Picker.Formatter provinceFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return list.get(i);
            }
        };
        mPicker.setFormatter(provinceFormatter);
        mPicker.setValue(20);
        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                widthDialog.hide();
            }
        });
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                widthDialog.hide();
                currentTileWidth = Integer.parseInt(list.get(mPicker.getValue()));
                widget.setTileWidthDp(currentTileWidth);
                if (date != null) {
                    widget.setSelectedDate(date);
                } else {
                    date = CalendarDay.today();
                    widget.setSelectedDate(date);
                    widget.clearSelection();
                    date = null;
                }
            }
        });

    }

    private void initHeightDialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_picker, null, false);
        heightDialog = initdialog(mComponent, this);
        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        Picker mPicker = (Picker) mComponent.findComponentById(ResourceTable.Id_picker);
        List<String> list = setData();
        mPicker.setMinValue(0);
        mPicker.setMaxValue(list.size() - 1);
        Picker.Formatter provinceFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return list.get(i);
            }
        };
        mPicker.setFormatter(provinceFormatter);
        mPicker.setValue(20);

        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                heightDialog.hide();
                currentTileHeight = Integer.parseInt(list.get(mPicker.getValue()));
                widget.setTileHeightDp(currentTileHeight);
                if (date != null) {
                    widget.setSelectedDate(date);
                } else {
                    date = CalendarDay.today();
                    widget.setSelectedDate(date);
                    widget.clearSelection();
                    date = null;
                }
            }
        });
        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                heightDialog.hide();
            }
        });

    }


    private List<String> setData() {
        List<String> list = new ArrayList<String>();
        for (int i = 24; i < 65; i++) {
            list.add(i + "");
        }
        return list;
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
        this.date = date;
    }

    private class TodayDecorator implements DayViewDecorator {

        private final CalendarDay today;
        private final Element backgroundDrawable;
        private final int color = 0x2244afcc;

        public TodayDecorator() {
            today = CalendarDay.today();
            backgroundDrawable = new ShapeElement(getContext(), ResourceTable.Graphic_background_val);
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return today.equals(day);
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.setBackgroundDrawable(backgroundDrawable);
        }
    }
}
